/**
* @project admin-go-api
* @name redisStore
* @descriptions redis 存取验证配置
* @author 寒锋
* @date 2023-12-13 16:07:37
 */

package util

import (
	"admin-go-api/constants"
	"admin-go-api/pkg/redis"
	"context"
	"log"
	"strings"
	"time"
)

var ctx = context.Background()

type RedisStore struct{}

// Set 存验证码
func (r RedisStore) Set(id string, value string) {
	key := constants.LoginCode + id
	err := redis.RedisDb.Set(ctx, key, value, 5*time.Minute).Err()
	if err != nil {
		log.Panicln(err.Error())
	}
}

// Get 获取验证码
func (r RedisStore) Get(id string, clear bool) string {
	key := constants.LoginCode + id
	value, err := redis.RedisDb.Get(ctx, key).Result()
	if err != nil {
		return ""
	}
	return strings.ToLower(value)
}

// Verify 校验验证码
func (r RedisStore) Verify(id string, answer string, clear bool) bool {
	v := RedisStore{}.Get(id, clear)
	return v == strings.ToLower(answer)
}
